import Phaser from 'phaser'
import star from '../../assets/star.png'
import platform from '../../assets/platform.png'
import sky from '../../assets/sky.png'
import dude from '../../assets/dude.png'


export default class DemoJumpScene extends Phaser.Scene {



    constructor() {
        super('demoJumpScene');
        this.player = null;
        this.platforms = null;
        this.cursors = null;
        this.starts = null
        this.score = 0
        this.scoreText = null
        this.bomps = null
        this.gameOver = false
    }

    preload() {
        this.load.image('star', star);
        this.load.image('platform', platform);
        this.load.image('sky', sky);
        this.load.spritesheet('dude', 
            dude,
            { frameWidth: 32, frameHeight: 48 }
        );
    }


    create() {
        this.add.image(400, 300, 'sky');
        this.add.image(400, 300, 'star');
        // 1. 创建一个平台，并启用静态物理身体（不受重力影响）
        this.platforms = this.physics.add.staticGroup()
        this.platforms.create(400, 568, 'platform').setScale(2).refreshBody();

        this.platforms.create(600, 400, 'platform');
        this.platforms.create(50, 250, 'platform');
        this.platforms.create(750, 220, 'platform');
        // 2. 创建玩家精灵，并启用动态物理身体
        this.player = this.physics.add.sprite(100, 0, 'dude'); 

        // 3. 设置玩家的物理属性
        this.player.setBounce(0.1); // 设置弹性，0为无弹性，1为完全弹性
        this.player.setCollideWorldBounds(true); // 禁止玩家飞出游戏世界边界
        this.player.body.setGravityY(300); 
        //设置动画
        this.anims.create({
            key: 'left',
            frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
            frameRate: 10,
            repeat: -1
        });
        
        this.anims.create({
            key: 'turn',
            frames: [ { key: 'dude', frame: 4 } ],
            frameRate: 20
        });
        
        this.anims.create({
            key: 'right',
            frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
            frameRate: 10,
            repeat: -1
        });

        
        this.stars = this.physics.add.group({
            key: 'star',
            repeat: 11,
            setXY: { x: 12, y: 0, stepX: 70 , stepY:24}
        });

        this.bomps = this.physics.add.group()

        this.stars.children.iterate(function (child) {

            child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));

        });
        this.scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });

        this.physics.add.collider(this.player, this.platforms);
        this.physics.add.collider(this.stars, this.platforms);
        this.physics.add.collider(this.bomps, this.platforms);
        this.physics.add.overlap(this.player, this.stars, this.collectStar, null, this);
        this.physics.add.collider(this.player, this.bomps, this.hitBomb, null, this);

        this.cursors = this.input.keyboard.createCursorKeys();
        // 4. 最关键的一步：让玩家和平台发生碰撞，而不是重叠
        // this.physics.add.collider(this.player,this.platforms);
    }

    update() {
        if (this.gameOver)
            {
                return;
            }
        
        if (this.cursors.left.isDown)
            {
                this.player.setVelocityX(-160);
            
                this.player.anims.play('left', true);
            }
            else if (this.cursors.right.isDown)
            {
                this.player.setVelocityX(160);
            
                this.player.anims.play('right', true);
            }
            else
            {
                this.player.setVelocityX(0);
            
                this.player.anims.play('turn');
            }
            
            if (this.cursors.up.isDown && this.player.body.touching.down)
            {
                this.player.setVelocityY(-330);
            }

        // 在更新函数里，我们可以通过修改速度来控制玩家移动
        // if (this.input.keyboard.addKey('RIGHT').isDown) {
        //     this.player.body.velocity.x = 160; // 设置x方向速度，向右移动
        // } else if (this.input.keyboard.addKey('LEFT').isDown) {
        //     this.player.body.velocity.x = -160; // 设置x方向速度，向右移动
        // }
    }
     collectStar (player, star)
    {
        star.disableBody(true, true);
        this.score += 10;
        this.scoreText.setText('Score: ' + this.score);

        if (this.stars.countActive(true) === 1)
            {
                //  A new batch of stars to collect
                this.stars.children.iterate(function (child) {
        
                    child.enableBody(true, child.x, 0, true, true);
        
                });
        
                let x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
        
                let bomb = this.bombs.create(x, 16, 'bomb');
                bomb.setBounce(1);
                bomb.setCollideWorldBounds(true);
                bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
                bomb.allowGravity = false;
        
            }
    }

    hitBomb(player, bomb) {
        this.physics.pause();

        this.player.setTint(0xff0000);
    
        this.player.anims.play('turn');
    
        this.gameOver = true;
    }

}



